using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameVuTru.ManagerScreens;
using GameVuTru.ResourceManager;
using GameVuTru.Screens;
namespace GameVuTru.GameObject
{
    public class Enemy : CObject
    {
        public enum EnemyType { SLIVER, YELLOW, RED, VITDO, VITVANG, VITXANHTROI, VITXANHCAY, VITHONG, STONE, BOSSRED };
        public int heath;
        public List<Bullet> listBullet;
        private Explore explosion;
        private ItemBonus bonus;
        public EnemyType m_typeenemy;
        public bool DieBecauseEatBullet;//chet vi an dan thi no vao tao ra Item, chet vi ra khoi man hinh thi ko co

        public bool Chosed;// True = bao hieu la duoc chon khi random 1 con tan cong xuong phi thuyen nguoi choi
        public Random rd;
        public Enemy(Vector2 pos, EnemyType typeenyme)
        {
            //AddBonus();
            vecloc = new Vector2(-3, 3);
            listBullet = new List<Bullet>();
            postion = pos;
            this.m_typeenemy = typeenyme;
            Chosed = false;
            rd = new Random();
            DieBecauseEatBullet = true;
            //bon UFO
            if (m_typeenemy == EnemyType.SLIVER)
            {
                m_sprite = new CSprite(ReSouce.SliverEnemy, 50, 5, 350, 700);
                heath = CreateLevel.level * 3;
            }
            else if (m_typeenemy == EnemyType.YELLOW)
            {
                m_sprite = new CSprite(ReSouce.yellowEnemy, 50, 5, 350, 700);
                heath = CreateLevel.level * 5;
            }
            else if (m_typeenemy == EnemyType.RED)
            {
                heath = CreateLevel.level * 7;
                m_sprite = new CSprite(ReSouce.redEnemy, 50, 5, 350, 700);
            }
            //Bon Vit
            else if (m_typeenemy == EnemyType.VITHONG)
            {
                heath = CreateLevel.level * 5;
                m_sprite = new CSprite(ReSouce.VitHong, 6, 6, 420, 70);
            }
            else if (m_typeenemy == EnemyType.VITDO)
            {
                heath = CreateLevel.level * 4;
                m_sprite = new CSprite(ReSouce.VitDo, 6, 6, 420, 70);
            }
            else if (m_typeenemy == EnemyType.VITXANHCAY)
            {
                heath = CreateLevel.level * 6;
                m_sprite = new CSprite(ReSouce.VitXanh, 6, 6, 420, 70);
            }
            else if (m_typeenemy == EnemyType.VITXANHTROI)
            {
                heath = CreateLevel.level * 7;
                m_sprite = new CSprite(ReSouce.VitXanhTroi, 6, 6, 420, 70);
            }
            else if (m_typeenemy == EnemyType.VITVANG)
            {
                heath = CreateLevel.level * 8;
                m_sprite = new CSprite(ReSouce.VitVang, 6, 6, 420, 70);
            }
            else if (m_typeenemy == EnemyType.STONE)
            {
                heath = CreateLevel.level;
                m_sprite = new CSprite(ReSouce.Stone, 16, 4, 200, 200);
            }
            else if (m_typeenemy == EnemyType.BOSSRED)
            {
                heath = 600;
                m_sprite = new CSprite(ReSouce.redBoss, 6, 6, 840, 140);
            }
            typeobject = OBJECTTYPE.ENEMY;
            isvisible = true;
            //AddBonus();
        }
        public override void Update(GameTime gametime)
        {
            //chet vi an boom va dan 
            if (heath <= 0 && isvisible && DieBecauseEatBullet)
            {
                isvisible = false;
                AddExplosion();
                AddBonus();

                //tang diem
                GameplayScreen.numScore += 100;

                //no nao`
                ManagerSound.PlaySound(ReSouce.soundExplosion);
            }
            //chet vi ra khoi man hjnh , o 1 so level nhat dinh, mjh da quy dinh o ben CreateLevel
            if (heath <= 0 && isvisible && !DieBecauseEatBullet)
            {
                isvisible = false;
                AddExplosion();
              
            }
            if (isvisible)
            {
                //bonus.postion = this.postion;
                if (m_typeenemy == EnemyType.RED || m_typeenemy == EnemyType.SLIVER || m_typeenemy == EnemyType.YELLOW)
                {
                    m_sprite.UpdateSprite(gametime, 0, 49, 1);
                }
                else if (m_typeenemy == EnemyType.BOSSRED || m_typeenemy == EnemyType.VITDO || m_typeenemy == EnemyType.VITHONG || m_typeenemy == EnemyType.VITVANG || m_typeenemy == EnemyType.VITXANHCAY || m_typeenemy == EnemyType.VITXANHTROI)
                {
                    m_sprite.UpdateSprite(222, gametime, 0, 5, 1);
                }
                else if (m_typeenemy == EnemyType.STONE)
                {
                    m_sprite.UpdateSprite(55, gametime, 0, 15, 1);
                }
                foreach (Bullet item in listBullet)
                {

                    item.Update(gametime);
                }
                base.Update(gametime);

            }
            else
            {
                if (bonus != null)
                {
                    bonus.Update(gametime);
                }
            }
            if (explosion != null)
            {
                explosion.Update(gametime);
            }

        }
        public override void Draw(SpriteBatch sp)
        {

            if (m_typeenemy == EnemyType.BOSSRED)
            {
                sp.Draw(ReSouce.heathBoss, new Vector2(100, 10), new Rectangle(100, 10, heath, 20), Color.White);
            }
            base.Draw(sp);
            if (isvisible)
            {
                m_sprite.DrawSprite(sp, postion);
                //draw bullet
                foreach (Bullet item in listBullet)
                {
                    item.Draw(sp);
                }
            }
            else
            {
                if (bonus != null)
                {
                    bonus.Draw(sp);
                }

            }
            if (explosion != null)
            {
                explosion.Draw(sp);
            }

        }
        public void AddBullet()
        {
            if (m_typeenemy == EnemyType.BOSSRED)
            {
                int soluong = rd.Next(3, 10);
                for (int i = 0; i < soluong; i++)
                {
                    int randx = rd.Next((int)(postion.X - m_sprite.widthsprite /2) ,(int)(postion.X + m_sprite.widthsprite));
                    int randy = rd.Next((int)(postion.Y - m_sprite.heightsprite / 2), (int)(postion.Y + m_sprite.heightsprite));
                    GameplayScreen.ListBulletEnemy.Add(new Bullet(BULLETTYPE.BULLETENEMY,new Vector2(randx,randy),0,new Vector2(0,8)));
                }
            }
            else
            {
              //  bl = new Bullet(BULLETTYPE.BULLETENEMY, postion, 0, new Vector2(0, 8));
                GameplayScreen.ListBulletEnemy.Add(new Bullet(BULLETTYPE.BULLETENEMY, postion, 0, new Vector2(0, 8)));
            }
            ManagerSound.PlaySound(ReSouce.soundUfoshoot);
        }
        //xu ly voi cac vu no cua enemy
        public void AddExplosion()
        {
            if (!isvisible)
            {
                if (m_typeenemy == EnemyType.SLIVER)
                {
                    explosion = new Explore(EXPLOSIONTYPE.SLIVEREXPLOSION, postion);

                }
                if (m_typeenemy == EnemyType.YELLOW)
                {
                    explosion = new Explore(EXPLOSIONTYPE.YELLOWEXPLOSION, postion);
                }
                if (m_typeenemy == EnemyType.RED)
                {
                    explosion = new Explore(EXPLOSIONTYPE.REDEXPLOSION, postion);
                }
                if (m_typeenemy == EnemyType.VITDO || m_typeenemy == EnemyType.VITVANG || m_typeenemy == EnemyType.VITHONG || m_typeenemy == EnemyType.VITXANHTROI || m_typeenemy == EnemyType.VITXANHCAY || m_typeenemy == EnemyType.STONE || m_typeenemy == EnemyType.BOSSRED)
                {
                    explosion = new Explore(EXPLOSIONTYPE.DUCKEXPLOSION, postion);
                }
            }

        }
        public void AddBonus()
        {
            Random r = new Random();
            int ran = r.Next(0, 70);
            switch (ran)
            {
                case 1:
                    bonus = new ItemBonus(postion, BONUSTYPE.THREECOLORBOX);
                    break;
                case 2:
                    bonus = new ItemBonus(postion, BONUSTYPE.REDBOX);
                    break;
                case 3:
                    bonus = new ItemBonus(postion, BONUSTYPE.BLUEBOX);
                    break;
                case 4:
                    bonus = new ItemBonus(postion, BONUSTYPE.SHIELD);
                    break;
                default:
                    bonus = new ItemBonus(postion, BONUSTYPE.COINBOX);
                    break;
            }

            //them bonus vao danh sach bonus ben game playing de xet va cham
            GameplayScreen.ListBonus.Add(bonus);
        }
        //remove vu no ra khoi list
        public override void moveRight(int x)
        {
            base.moveRight(x);
            postion.X += x;
        }
        public override void moveleft(int x)
        {
            base.moveleft(x);
            postion.X -= x;
        }
        public override void moveTop(int x)
        {
            base.moveTop(x);
            postion.Y -= x;
        }
        public override void moveBottom(int x)
        {
            base.moveBottom(x);
            postion.Y += x;
        }
    }
}
